import 'dart:convert';

import 'package:client_ui/model/ConnectModel.dart';

import 'package:json_annotation/json_annotation.dart';

import '../../model/SendMsgModel.dart';
import '../../model/rest/RestCode.dart';
import '../../model/rest/RestResult.dart';
import '../../util/GenerateUtil.dart';
import '../Request.dart';

class ConnectApi {
  static connect(int id) {
    ConnectModel connectModel = ConnectModel(id: id);
    RestResult result = new RestResult(
        code: RestCode.connect.code,
        msg: RestCode.connect.msg,
        data: connectModel.toJson());

    Request.getInstance()?.channel?.sink.add(jsonEncode(result.toJson()));
  }

  static addChatMsg(RestResult result, globalData) {
    SendMsgModel sendMsgModel = SendMsgModel.fromJson(result.data);
    globalData.addChatDisplayList(GenerateUtil.generateChatDisplayBox(
        sendMsgModel.msg,
        sendMsgModel.avatarUrl,
        sendMsgModel.isSelf));
  }

  static sendChatMsg(int fromId, String avatarUrl, String msg) {
    SendMsgModel sendMsgModel = SendMsgModel(fromId: fromId, avatarUrl: avatarUrl, msg: msg);
    RestResult result = new RestResult(
        code: RestCode.sendMsg.code,
        msg: RestCode.sendMsg.msg,
        data: sendMsgModel.toJson());

    Request.getInstance()?.channel?.sink.add(jsonEncode(result.toJson()));
  }
}
